
function test_init() {
	var T= new G.Test.TestSuit("Test Suit for Datagrid", "center");
	var dgrid = G('TEST_GRID').DataGrid([
		{type: 'select', width: 40},
		{type: 'index', name: 'index', width: 60},
		{type: 'data', name: 'COL-01', width:100, key: "key1"},
		{type: 'data', name: 'COL-02', width:100, key: "key2"},
		{type: 'data', name: 'COL-03', width:100, key: "key3"}
	]);

	var data_source_init = function(count) {
		var testArray = [];
		for (var i = 0; i < count; i++) {
			testArray[i] = { key1: i, key2: i + "-1", key3: i + "-2"};
		}
		return new G.Widget.DataSourceArray(testArray);
	}

	var dgrid_test_page_total = function(dg) 
	{
		var test_values = [
			[0, 1],
			[1,1],
			[99,1],
			[100,1],
			[101, 2],
			[199, 2],
			[200, 2],
			[201, 3],
			[999, 10]
		];
		var len = test_values.length;

		for (var i = 0; i <len; i++) {
			var tv = test_values[i];
			var ds = data_source_init(tv[0]);
			dgrid.link_data(ds);
			this.assert_equ(dgrid.page_total(), tv[1]);
		}
	}

	var dgrid_test_current_page = function(dg)
	{
		var ds = data_source_init(100);
		dgrid.link_data(ds);
		dgrid.set_page_size(10);

		for (i = 0; i < 11; i++) {
			if (i < 10) {
				this.assert_equ(dgrid.current_page(), i + 1);
				this.assert_equ(dgrid.divPage.value, i + 1 + "/10");
			} else {
				this.assert_equ(dgrid.current_page(), 10);
				this.assert_equ(dgrid.divPage.value, "10/10");
			}
			dgrid.next_page();
		}
	}

	var dgrid_check_page = function(pgSize, pgTotal,  pg) {

		var msg = "";
		if (dgrid.current_page() != pg) {
			msg = "current_page error: expect " + pg + ", got " + dgrid.current_page();
			return msg;
		};

		var row = dgrid.rows[0];
		var dat = row.childNodes[2].innerHTML;

		if (dat != ((pg - 1) * pgSize)) {
			msg = "first data error: expect " + ((pg - 1) * pgSize) 
					+ ", got " + dat;
			return msg;
		}

		var pgNav = pg + "/" + pgTotal;

		if (dgrid.divPage.value != (pg + "/" + pgTotal)) {
			msg = "page number in nav error: expect " + pgNav 
					+ ", got " + dgrid.divPage.value;
		}
		return msg;
	}
	var dgrid_test_first_last_page = function()
	{
		dgrid.set_page_size(10);
		for (t = 1; t < 41; t++) {
			var pageCount = Math.floor((t + 9) / 10);
			var ds = data_source_init(t);
			dgrid.link_data(ds);
			dgrid.last_page();
			err = dgrid_check_page(10, pageCount, pageCount);
			this.assert((err.length == 0), err);

			dgrid.first_page();
			err = dgrid_check_page(10, pageCount, 1);
			this.assert((err.length == 0), err);
		}
	}

	var dgrid_test_next_page = function()
	{
		var PG_SIZE = 6;
		dgrid.set_page_size(PG_SIZE);
		for (t = 1; t < 41; t++) {
			var pageCount = Math.floor((t + PG_SIZE - 1) / PG_SIZE);

			var ds = data_source_init(t);
			dgrid.link_data(ds);
			for (var pg = 0; pg < pageCount + 1; pg++) {
				if (pg < pageCount) {
					err = dgrid_check_page(PG_SIZE, pageCount, pg+1);
				} else {
					err = dgrid_check_page(PG_SIZE, pageCount, pageCount);
				}
				this.assert((err.length == 0), err);
				dgrid.next_page();
			}
		}
	}

	var dgrid_test_prev_page = function()
	{
		var PG_SIZE = 6;
		dgrid.set_page_size(PG_SIZE);
		for (t = 1; t < 41; t++) {
			var pageCount = Math.floor((t + PG_SIZE - 1) / PG_SIZE);
			var ds = data_source_init(t);
			dgrid.link_data(ds);
			dgrid.last_page();
			for (var pg = pageCount; pg > 0; pg--) {
				err = dgrid_check_page(PG_SIZE, pageCount, pg);
				this.assert((err.length == 0), err);
				dgrid.prev_page();
			}
		}
	}

	var dgrid_check_row_view = function(curr) 
	{
		var err = "";
		var rowH = dgrid.rows[0].offsetHeight;
		var bodyH = dgrid.divBodyView.clientHeight;
		var rowCount = Math.floor((bodyH + rowH + dgrid.scrollOffset - 1) / rowH);
		
		if (rowCount != dgrid.rows.length) {
			msg = "count of row in view error: expect " + rowCount
					+ ", got " + dgrid.rows.length;
			return msg;
		}

		var pg = dgrid.current_page();
		var pgSize = dgrid.pageSize;

		// Check row data.
		for (var i = 0; i < rowCount; i++) {
			var dat = (pg * pgSize) + dgrid.viewOffset;
			if (dgrid.rows[i].childNodes[2].innerHTML != dat) {
				msg = "row " + i + " error: data expect: " + dat
					+ " got: " + dgrid.rows[i].childNodes[2].innerHTML;
			}
		}

		// The current row must be displayed completely.
		var i = curr - dgrid.viewOffset;
		if (i == 0) {
			if (dgrid.scrollOffset != 0) {
				msg = "current row is displayed on top of view area, but is not displayed completely:";
				return msg;
			}
		} else if (((i + 1) * dgrid.rowHeight - dgrid.scrollOffset) > dgrid.viewHeight) {
			msg = "current row is out of view area. ";
			return msg;
		}
		if (typeof(dgrid.rows[i].className) == 'undefined' || dgrid.rows[i].className != "current") {
			msg = "current row " + i + " is not highlighed";
			return msg;
		}
		return err;
	}


	/*
	 * 1. set_current can move the highlight line only in current page.
	 * 2. There are only 1 highlight line in page.
	 * 3. If the line was set not current, the color will recover to show 
	 *    it's selection status.
	 */

	var dgrid_test_set_current = function() 
	{
		var PG_SIZE = 20;
		dgrid.set_page_size(PG_SIZE);

		var ds = data_source_init(50);
		dgrid.link_data(ds);

		for (var i = 0; i < PG_SIZE; i++) {
			dgrid.set_current(i);
			err = dgrid_check_row_view(i);
			this.assert((err.length == 0), err);
		}

		for (var i = (PG_SIZE - 1); i >= 0; i--) {
			dgrid.set_current(i);
			err = dgrid_check_row_view(i);
			this.assert((err.length == 0), err);
		}
	}

	var dgrid_test_performance_10000 = function()
	{
		var PG_SIZE = 50000;
		dgrid.set_page_size(PG_SIZE);
		var ds = data_source_init(50000);
		dgrid.link_data(ds);
	}


	T.add_case(new G.Test.TestCase('G.DataGrid.page_total', null, dgrid_test_page_total));
	T.add_case(new G.Test.TestCase('G.DataGrid.current_page', null, dgrid_test_current_page));
	T.add_case(new G.Test.TestCase('G.DataGrid.first_last_page', null, dgrid_test_first_last_page));
	T.add_case(new G.Test.TestCase('G.DataGrid.next_page', null, dgrid_test_next_page));
	T.add_case(new G.Test.TestCase('G.DataGrid.prev_page', null, dgrid_test_prev_page));
	T.add_case(new G.Test.TestCase('G.DataGrid.set_current', null, dgrid_test_set_current));
	T.add_case(new G.Test.TestCase('G.DataGrid.performance', null, dgrid_test_performance_10000));
	T.run();
}

